<?php

/**
 * MyProjectNameHere <http://www.example.com>
 * Model Class
 *
 * It is recommended to extend Model classes from WWW_Factory in order to 
 * provide various useful functions and API access for the Model.
 *
 * @package    Factory
 * @author     DeveloperNameHere <email@example.com>
 * @copyright  Copyright (c) 2012, ProjectOwnerNameHere
 * @license    Unrestricted
 * @tutorial   /doc/pages/guide_mvc.htm
 * @since      1.0.0
 * @version    1.0.0
 */

class WWW_model_banners extends WWW_Factory {

	/**
	 * It is recommended to define all data variables here. Usually the 
	 * data variables have the same names as the column names of database 
	 * rows from a table.
	 */	
	public $conn;
	/**
	 * Alternative to __construct()
	 *
	 * WWW_Factory does not allow to overwrite __construct() method, so 
	 * this __initialize() is used instead and loaded automatically when 
	 * object is created.
	 *
	 * @return boolean
	 */
	public function __initialize()
	{
		require('engine/class.www-database.php');
		$this->conn=new WWW_Database();
		require('engine/include_dao.php');
		return true;
		
	}
	public function create_banner($data)
	{
			$file = $data['banner_image']['tmp_name'];
			$filename = time().$data["banner_image"]['name'];
			$banner_image_path="uploads/banners/".$filename;
			move_uploaded_file($file, $banner_image_path);
		
	$query="insert into `banners` (banner_name,banner_view,sequence,banner_image,banner_url,featured,event_block,classifieds,image_name,created_date,updated_date) VALUES ('".$data['banner_name']."','".$data['banner_view']."','".$data['sequence']."','".$this->conn->baseurl().$banner_image_path."','".$data['banner_url']."','".$data['featured']."','".$data['event_block']."','".$data['classifieds']."','".$data["banner_image"]['name']."','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."')";
		//print_r($query);exit;
	$result=$this->conn->dbCommand($query);	
		//$this->db->last_query();exit;
	if($result){
			return array('message'=>'subModule successfully created');			
		}else{
			return array('message'=>'submodule Creation Failed');
		}	
	}
	public function edit_banner($data)
	{	
			if($data['banner_image']['tmp_name']!="")
			{
			$file = $data['banner_image']['tmp_name'];
			$filename = time().$data["banner_image"]['name'];
			$banner_image_path="uploads/banners/".$filename;
			move_uploaded_file($file, $banner_image_path);
			}
			else
			{
				$banner_image_path = "";
				$data["banner_image"]['name'] = "";
			}
			//print_r($this->conn->baseurl().$banner_image_path);exit;
			$query="update `banners` set banner_name='".$data['banner_name']."',banner_view='".$data['banner_view']."',sequence='".$data['sequence']."'".($banner_image_path!="" ? ",banner_image='".$this->conn->baseurl().$banner_image_path : '')."',banner_url='".$data['banner_url']."',featured='".$data['featured']."',event_block='".$data['event_block']."',classifieds='".$data['classifieds']."'".($data["banner_image"]['name']!="" ? ",image_name='".$data["banner_image"]['name'] : '')."',updated_date='".date("Y-m-d H:i:s")."' where banner_id=".$data['banner_id'];
			//print_r($query);exit;
			$result=$this->conn->dbCommand($query);		
			if($result){
				return array('message'=>'Updated Successfully');			
			}else{
				return array('message'=>'Updates Failed');
			}	
	
	}
	
	public function get_banner($data)
	{
		//print_r($data);exit;
	$query="SELECT * FROM banners where banner_id='".$data['banner_id']."'";
		//print_r($query);exit;
		$result=$this->conn->dbMultiple($query);		
		if($result){
			return array('message'=>'business type data','result'=>$result);			
		}else{
			return array('message'=>'business by id is failed');
		}	
	
	}
	public function delete_banner($data)
	{
		$query="update `banners` set status='".$data['status']."' where banner_id='".$data['banner_id']."'";
		$data=$this->conn->dbCommand($query);
		if($data)
			return array('message'=>"business Deleted Successfully");		
		else
			return array('message'=>'business Deletion Fails');
	}
	
	
	
	public function all_banners($data)
	{	
	$current_page=0;
	$results_per_page=10;
	$order_by="created_date";
	$sort_order="desc";
	if(isset($data['order_by']) && $data['order_by']!='')
	{
		$order_by=$data['order_by'];
	}
	if(isset($data['sort_order']) && $data['sort_order']!='')
	{
		$sort_order=$data['sort_order'];
	}
	if(isset($data['current_page'])&& $data['current_page']!='' || $data['current_page']!=0)
	{
	$current_page=$data['current_page'];
	}
	if(isset($data['results_per_page']) && $data['results_per_page']!='' || $data['results_per_page']!=0){
	$results_per_page=$data['results_per_page'];
	}
	$offset=$current_page;
	$query_string="SELECT * FROM banners";
	$query_string1="select count(*) as total FROM banners";
	if (isset($data['search_by']) && $data['search_by']!='')
	{	//print_r($data);exit;
		
		
		if($data['search_by'] == 'banner_name')
		{
				$search_by = "banner_name";
		}else if($data['search_by'] == 'image_name')
		{
			$search_by = "image_name";
		}
		$query =$query_string." where ".$search_by." like '%".$data['search_by_value']."%' order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
		$query1 =$query_string1." where ".$search_by." like '%".$data['search_by_value']."%'";
		
	}else
	{
	$query=$query_string." order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
	$query1 = $query_string1;
	}	
		//print_r($query1);exit;
		$result=$this->conn->dbMultiple($query);
		if($result){
			$totalrows = $this->conn->dbSingle($query1);
		return array('message'=>"All ","info"=>$result,'total'=>$totalrows['total'],'current_page'=>$current_page,'results_per_page'=>$results_per_page);
		}
		else
		return array('message'=>'No Banners');
		
	}
}
	
?>